<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script src="./js/03-ajax.js"></script>
  <script>
    // 系统帮助我们封装好了
    //   console.log(Promise.resolve('success'))
    //   Promise.resolve('success').then(value => {
    //     console.log(value)
    //   })
    // Promise.fn = function (value) {
    //   return new Promise(resolve => {
    //     resolve(value)
    //   })
    // }
    // console.log(Promise.fn('lisa'))
    // Promise.fn('lisa').then(value => {
    //   console.log(value)
    // })

    // function fn() { }
    // fn.site = 'url'
    // console.dir(fn)

    function query(name) {
      const cache = query.cache || (query.cache=new Map());
      if(cache.has(name)) {
        console.log('走缓存了')
        return Promise.resolve(cache.get(name))
      }

      return ajax('http://localhost:3000/posts').then(user => {
        cache.set(name, user);
        console.log('没走缓存')
        return user
      })
    }
    query('lisa').then(user => {
      console.log(user)
    })

    setTimeout(() => {
      query('lisa').then(user => {
        console.log(user)
      })
    }, 1000)
  </script>
</body>

</html>